package com.itmuch.contentcenter.controller.content;

import com.github.pagehelper.PageInfo;
import com.itmuch.contentcenter.auth.CheckLogin;
import com.itmuch.contentcenter.domain.dto.content.ShareDTO;
import com.itmuch.contentcenter.domain.entity.content.Share;
import com.itmuch.contentcenter.service.content.ShareService;
import com.itmuch.contentcenter.utils.JwtOperator;
import io.jsonwebtoken.Claims;
import org.apache.logging.log4j.util.Strings;
import org.aspectj.apache.bcel.classfile.Module;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/shares")
public class ShareController {

    @Resource
    private ShareService shareService;

    @Resource
    private JwtOperator jwtOperator;

    @GetMapping("/{id}")
    @CheckLogin
    public ShareDTO findById(@PathVariable Integer id){
     return this.shareService.findById(id);
    }

    @GetMapping("/q")
    public PageInfo<Share> q(
           @RequestParam(required = false) String title,
            @RequestParam(required = false,defaultValue = "1") Integer pageNo,
            @RequestParam(required = false,defaultValue = "10") Integer pageSize,
           @RequestHeader(value = "Token-X",required = false) String token
    ){

           pageSize = pageSize>100?100:100;
        Integer userId = null;
        if (Strings.isNotBlank(token)){
            Claims fromToken = jwtOperator.getClaimsFromToken(token);
            userId = (Integer)fromToken.get("id");
        }

            return this.shareService.q(title,pageNo,pageSize,userId);
    }

    @GetMapping("/exchange/{id}")
    @CheckLogin
    public Share exchangeId(@PathVariable("id") Integer id,HttpServletRequest request){
        return this.shareService.exchangeId(id,request);
    }

}
